// pages/roomPattern/roomDetail.js
var util = require('../../utils/util.js');
var app = getApp();

Page({

  /**
   * 页面的初始数据
   */
  data: {
    userInfo: '', // 登录用户信息
    id: '', // 房源编号
    oid: '', // 租约编号
    rm: '', // 房源详情
    reletOrder: '', // 续租信息
    stateOperateHidden: true, // 状态操作弹出框状态
    stateTitle: '', // 状态操作弹出框标题
    room_show: '', // 房源临时状态
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    var that = this;
    var id = options.id; // 房源编号
    var oid = options.oid; // 租约编号
    wx.getStorage({
      key: 'userInfo',
      success: function(res) {
        var data = res.data;
        that.setData({
          userInfo: data,
          id: id,
          oid: oid
        });
        that.getRoomDetail();
      },
      fail: function() {
        wx.redirectTo({
          url: '/pages/index/index',
        })
      }
    });
  },

  /**
   * 获取房间详情信息
   */
  getRoomDetail: function() {
    wx.showLoading({
      title: '加载中···'
    });
    var that = this;
    var data = that.data;
    var source = data.userInfo.source; // 房东ID
    var id = data.id; // 房源ID
    var oid = data.oid; // 租约ID
    util.getReq('/app/home/getRoomDetail', {
      source: source,
      'term.room_id': id,
      'order.order_id': oid
    }, function(data) {
      if (data.code == 0) {
        var params = data.params;
        that.setData({
          rm: params.roomPattern
        });
        var reletOrder = params.reletOrder;
        var newReletOrder = [];
        for (var i = 0; i < reletOrder.length; i++) {
          var s = {
            'relet_begin_time': util.format(reletOrder[i].relet_begin_time, 'yyyy-MM-dd'),
            'relet_end_time': util.format(reletOrder[i].relet_end_time, 'yyyy-MM-dd'),
            'confirm_state': reletOrder[i].confirm_state
          };
          newReletOrder.push(s);
        }
        that.setData({
          reletOrder: newReletOrder,
        });
      } else {
        util.isError(data.desc);
      }
      wx.hideLoading();
    });
  },

  /**
   * 修改出租状态
   */
  onEditLeaseState: function() {
    var that = this;
    var room_show = that.data.rm.room_show;
    if (room_show == 2) {
      that.setData({
        stateTitle: '未租状态',
        stateOperateHidden: false,
        room_show: 1
      });
    } else {
      that.setData({
        stateTitle: '已租状态',
        stateOperateHidden: false,
        room_show: 2
      });
    }
  },

  /**
   * 修改展示状态
   */
  onEditShowState: function() {
    var that = this;
    var room_show = that.data.rm.room_show;
    if (room_show == 1) {
      that.setData({
        stateTitle: '对外隐藏',
        stateOperateHidden: false,
        room_show: 0
      });
    } else {
      that.setData({
        stateTitle: '对外展示',
        stateOperateHidden: false,
        room_show: 1
      });
    }
  },

  /**
   * 确定
   */
  onConfirm: function() {
    var that = this;
    that.setData({
      stateOperateHidden: true
    });
    that.onEditState();
  },

  /**
   * 取消
   */
  onCancel: function() {
    var that = this;
    that.setData({
      stateOperateHidden: true
    });
  },

  /**
   * 修改该房源状态
   */
  onEditState: function() {
    var that = this;
    var data = that.data;

    var room_show = data.room_show; // 房间临时状态
    var stateTitle = data.stateTitle; // 操作文本

    var userInfo = data.userInfo; // 登录信息
    var uid = userInfo.user_id; // 登录人ID
    var source = userInfo.source; // 登录人房东ID
    var userName = userInfo.user_name; // 登录人名称

    var roomInfo = data.rm; // 房间信息
    var room_id = roomInfo.room_id; // 房源编号
    var room_code = roomInfo.room_code; // 房间号
    var apartment_name = roomInfo.apartment_name; // 公寓名
    var village_name = roomInfo.village_name; // 小区名
    util.getReq('/app/home/updateRoomState', {
      'uid': uid,
      'source': source,
      'showText': stateTitle,
      'userName': userName,
      'room.room_show': room_show,
      'room.room_id': room_id,
      'room.room_code': room_code,
      'room.apartment_name': apartment_name,
      'room.village_name': village_name
    }, function(data) {
      if (data.code == 0) {
        wx.showToast({
          title: '修改成功',
          icon: 'success',
          duration: 1000
        });
        setTimeout(function() {
          that.getRoomDetail();
        }, 1000);
      } else {
        util.isError(data.desc);
      }
    });
  },

  /**
   * 编辑房源
   */
  editRoom: function(){
    wx.navigateTo({
      url: 'editRoom?id=' + this.data.id
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function() {

  }
})